package com.xyy.saas.payment.adpater.assemble;

import com.xyy.saas.payment.adpater.assemble.dto.WithdrawAccountDto;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.model.ThirdUserInfo;
import com.xyy.saas.payment.merchant.core.service.UserInfoService2;
import com.xyy.saas.payment.merchant.core.vo.UserInfoVo;
import com.xyy.saas.payment.service.ThirdUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
@Slf4j
public class FBankRequestParamAssemble {
    @Resource
    private UserInfoService2 userInfoService2;
    @Resource
    private ThirdUserInfoService thirdUserInfoService;

    /**
     * 获取提现账户信息
     * @param accountId
     * @return
     */
    public WithdrawAccountDto getWithdrawAccountInfo(String accountId) throws PaymentException {
        UserInfoVo userInfoVo = null;

        try {
            userInfoVo = userInfoService2.queryUserInfo(accountId);
        }
        catch(Exception e) {
            log.error("FBankRequestParamAssemble#getWithdrawAccountInfo userInfo decrypt error accountId:{}", accountId);
        }

        if(userInfoVo == null) {
            throw new PaymentException(ResultCodeEnum.USER_INFO_DECRYPT_ERROR);
        }

        WithdrawAccountDto result = new WithdrawAccountDto();
        result.setPayeeName(userInfoVo.getSettleInfo().getRecipient().getName());
        result.setPayeeAccountNo(userInfoVo.getSettleInfo().getRecipient().getAccount());

        ThirdUserInfo thirdUserInfo = thirdUserInfoService.getThirdUserInfoByUserId(accountId);

        if(thirdUserInfo == null) {
            throw new PaymentException(ResultCodeEnum.THIRD_USER_ID_NOT_EXIST);
        }

        result.setPayerName(userInfoVo.getSettleInfo().getRecipient().getName());
        result.setPayerAccountNo(thirdUserInfo.getThirdUserId());
        return result;
    }
}
